package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.hzn.lib.EasyPullLayout;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.topscore.uke.R;
import com.topscore.uke.entities.TeamStaffEntity;
import com.topscore.uke.mvp.contractors.GetGroupStaffContractor;
import com.topscore.uke.mvp.presenters.GetGroupStaffListPresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class TeamStaffListActivity extends ToolBarCompatActivity implements GetGroupStaffContractor.View {

    @Bind(R.id.root_recycler)
    RecyclerView rootRecycler;
    @Bind(R.id.pull_layout)
    EasyPullLayout pullLayout;

    private MultiAdapter mMultiAdapter;

    private final int _Type_Manager = 1001, _Type_Staff = 1002;

    private List<MultiItem> mItemList;

    private BaseQuickAdapter<TeamStaffEntity, BaseViewHolder> mManagerAdapter, mStaffAdapter;

    private GetGroupStaffListPresenter mListPresenter;

    private LoadService mLoadService;

    private Callback loadingCallBack, errorCallBack;

    private String mGroupCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_staff_list);
        ButterKnife.bind(this);
        mGroupCode = "m1003";
        mListPresenter = new GetGroupStaffListPresenter(mGroupCode, this);
        setCentralTitle("成员");
        initViews();
    }

    private void initViews() {
        initAdapters();
        pullLayout.setOnTriggerListener(integer -> {
            loadMore();
            return null;
        });
        rootRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rootRecycler.setAdapter(mMultiAdapter);
        initData();
    }

    private void loadMore() {

    }

    private void initData() {
        mListPresenter.getStaffList();
    }

    private void initAdapters() {
        mManagerAdapter = new BaseQuickAdapter<TeamStaffEntity, BaseViewHolder>(R.layout.team_staff_item, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder holder, TeamStaffEntity item) {
                holder.setText(R.id.name_tv, item.getEmpName());
                holder.getConvertView().setOnClickListener(v -> toStaffInfo(item.getId()));
            }
        };
        mStaffAdapter = new BaseQuickAdapter<TeamStaffEntity, BaseViewHolder>(R.layout.team_staff_item, new ArrayList<>()) {
            @Override
            protected void convert(BaseViewHolder holder, TeamStaffEntity item) {
                holder.setText(R.id.name_tv, item.getEmpName());
                holder.getConvertView().setOnClickListener(v -> toStaffInfo(item.getId()));
            }
        };

        mItemList = new ArrayList<>();
        final int[] items = {_Type_Manager, _Type_Staff};
        for (int i = 0; i < items.length; i++) {
            MultiItem item = new MultiItem(items[i]);
            mItemList.add(item);
        }
        mMultiAdapter = new MultiAdapter(mItemList);

    }

    private void toStaffInfo(String id) {
        Intent intent = new Intent(mContext, StaffInformationActivity.class);
        intent.putExtra("groupCode", mGroupCode);
        intent.putExtra("userId", id);
        startActivity(intent);
    }

    @Override
    public void onLoading(String loadingMessage) {
        showLoadingDialog(loadingMessage);
    }

    @Override
    public void onLoadFinished() {
        dismissLoadingDialog();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
    }

//    @Override
//    public void onLoadStaffListSuccess(List<TeamStaffEntity> list) {
//        List<TeamStaffEntity> managerList = new ArrayList<>();
//        List<TeamStaffEntity> staffList = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).getIsCaptain() != null && list.get(i).getIsCaptain().equals("1")) {
//                managerList.add(list.get(i));
//            } else {
//                staffList.add(list.get(i));
//            }
//        }
//        mManagerAdapter.setNewData(managerList);
//        mStaffAdapter.setNewData(staffList);
//    }

//    @Override
//    public void onLoadNextPageSuccess(List<TeamStaffEntity> list) {
//
//    }

    @Override
    public BaseQuickAdapter getCaptainAdapter() {
        return mManagerAdapter;
    }

    @Override
    public BaseQuickAdapter getStaffListAdapter() {
        return mStaffAdapter;
    }

    @Override
    public View getLoadingFooterView() {
        return null;
    }

    @Override
    public View getErrorFooterView() {
        return null;
    }

    private class MultiItem implements MultiItemEntity {

        private int itemType;

        private MultiItem(int itemType) {
            this.itemType = itemType;
        }

        @Override
        public int getItemType() {
            return itemType;
        }
    }

    private class MultiAdapter extends BaseMultiItemQuickAdapter<MultiItem, BaseViewHolder> {

        /**
         * Same as QuickAdapter#QuickAdapter(Context,int) but with
         * some initialization data.
         *
         * @param data A new list is created out of this one to avoid mutable list
         */
        public MultiAdapter(List<MultiItem> data) {
            super(data);
            addItemType(_Type_Manager, R.layout.team_taff_managers_layout);
            addItemType(_Type_Staff, R.layout.team_staff_layout);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, MultiItem item) {
            switch (item.getItemType()) {
                case _Type_Manager:
                    RecyclerView managerRecycler = viewHolder.getView(R.id.staff_recycler);
                    managerRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
                    managerRecycler.setAdapter(mManagerAdapter);
                    break;
                case _Type_Staff:
                    RecyclerView staffRecycler = viewHolder.getView(R.id.staff_recycler);
                    staffRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
                    LoadMoreView loadMoreView = new LoadMoreView() {
                        @Override
                        public int getLayoutId() {
                            return R.layout.quick_view_load_more;
                        }

                        @Override
                        protected int getLoadingViewId() {
                            return R.id.load_more_loading_view;
                        }

                        @Override
                        protected int getLoadFailViewId() {
                            return R.id.load_more_load_fail_view;
                        }

                        @Override
                        protected int getLoadEndViewId() {
                            return R.id.load_more_load_end_view;
                        }
                    };
                    mStaffAdapter.setLoadMoreView(loadMoreView);
                    mStaffAdapter.setEnableLoadMore(true);
                    mStaffAdapter.setOnLoadMoreListener(new RequestLoadMoreListener() {
                        @Override
                        public void onLoadMoreRequested() {
                            mListPresenter.loadNextPage();
                        }
                    }, staffRecycler);
                    staffRecycler.setAdapter(mStaffAdapter);
                    break;
            }
        }
    }
}
